import 'dart:async';

import 'package:cunyutech_flutter_ui/src/theme/main.dart';
import 'package:flutter/material.dart';

import 'OverlaySupport.dart';

enum OverlayConfirmActionType {
  CANCEL,
  OK,
}

class OverlayConfirmAction {
  OverlayConfirmAction({ this.text, this.type = OverlayConfirmActionType.OK });
  String text;
  OverlayConfirmActionType type;
}

Future<int> showOverlayConfirm({
  String title,
  String content,
  Widget child,
  List<OverlayConfirmAction> actions,
  Color maskColor,
  Color borderColor,
}) {
  assert(content == null || child == null, "不能同时传递child和content");
  actions ??= [
    OverlayConfirmAction(
      text: "确定"
    )
  ];
  var width = CYScreen.width - 46*2;
  var btnWidth = width / actions.length;
  var _borderColor = borderColor ?? Color(0xFF979797);

  Completer<int> completer = Completer<int>();
  OverlayEntry overlay;
  overlay = showOverlay(
    builder: (BuildContext _, OverlaySupportState state) {
      return Container(
        alignment: Alignment.center,
        color: maskColor ?? Colors.black.withOpacity(0.6),
        padding: EdgeInsets.symmetric(
          horizontal: 46
        ),
        child: Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(2)
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Container(
                padding: EdgeInsets.symmetric(horizontal: CYMargin.Default, vertical: CYHeight.Default),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Text.rich(
                      TextSpan(text: title ?? "提示", style: TextStyle(
                        fontSize: CYFontSize.Large,
                        height: 24 / CYFontSize.Large,
                        color: Colors.black,
                        fontWeight: FontWeight.bold
                      )),
                      textAlign: TextAlign.center,
                    ),
                    child != null || content != null ? SizedBox(height: CYMargin.Default,) : Container(),
                    content != null ? 
                      Text.rich(
                        TextSpan(text: content ?? content, style: TextStyle(
                          fontSize: CYFontSize.Middle,
                          height: 21 / CYFontSize.Middle,
                          color: Color(0xFF666666),
                        )),
                        textAlign: TextAlign.center,
                      ) :
                      Container(),
                    child != null ? child : Container(),
                  ],
                )
              ),
              Divider(height: 1, color: _borderColor),
              Row(
                children: actions.map((e) {
                  var index = actions.indexOf(e);
                  return GestureDetector(
                    onTap: () {
                      overlay.remove();
                      completer.complete(index);
                    },
                    child: Container(
                      width: btnWidth,
                      height: CYHeight.Large,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        border: index != 0 ? Border(left: BorderSide(width: 0.5, color: _borderColor)) : null
                      ),
                      child: Text.rich(
                        TextSpan(text: e.text, style: TextStyle(
                          fontSize: CYFontSize.Middle,
                          height: 21 / CYFontSize.Middle,
                          color: e.type == OverlayConfirmActionType.OK? Colors.black : Color(0xFF666666),
                        ))
                      ),
                    )
                  );
                }).toList()
              )
            ],
          )
        )
      );
    }
  );
  return completer.future;
} 